# -*- coding:utf-8 -*-
import struct
import numpy as np
import glob

def get_data(root,shuffle=True):
	assert root in ['训练集','测试集'],'只有训练集和测试集'
	if root == '训练集':
		data_size = 144
	else:
		data_size = 18
	features = []
	for i in range(200):
		file = '数据集/%s/f%d.dat'%(root,i)
		with open(file,'rb') as f:
			data_raw = struct.unpack('f'*(data_size*440),f.read(4*data_size*440))
			data = np.asarray(data_raw).reshape(-1,440)
		features.append(data)
	labels = []
	for i in range(200):
		labels.append((i+1)*np.ones((data_size),dtype = np.int32))
	# 转为numpy数组
	features = np.concatenate(features,axis = 0)
	labels = np.concatenate(labels,axis = 0)
	
	# 打乱数据
	if shuffle:
		data_index = np.arange(labels.shape[0])
		np.random.shuffle(data_index)
		features = [features[i] for i in data_index]
		labels = [labels[i] for i in data_index]
		features=np.array(features)
		labels=np.array(labels)
	return features,labels
